home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / By the Book / Mac Pascal Primer, 4.0 / Chap 4, EventTutor ƒ / EventTutor.p next >
Text File  |  1990-07-13  |  8KB  |  304 lines

  1. program EventTutor;
  2.     const
  3.         BASE_RES_ID = 400;
  4.         LEAVE_WHERE_IT_IS = FALSE;
  5.         NORMAL_UPDATES = TRUE;
  6.         SLEEP = 60;
  7.         WNE_TRAP_NUM = $60;
  8.         UNIMPL_TRAP_NUM = $9F;
  9.         SUSPEND_RESUME_BIT = $0001;
  10.         ACTIVATING = 1;
  11.         RESUMING = 1;
  12.         TEXT_FONT_SIZE = 12;
  13.         DRAG_THRESHOLD = 30;
  14.         MIN_WINDOW_HEIGHT = 50;
  15.         MIN_WINDOW_WIDTH = 50;
  16.         SCROLL_BAR_PIXELS = 15;
  17.         ROWHEIGHT = 15;
  18.         LEFTMARGIN = 10;
  19.         STARTROW = 0;
  20.         HORIZONTAL_OFFSET = 0;
  21.  
  22.     var
  23.         gPictWindow, gEventWindow: WindowPtr;
  24.         gDone, gWNEImplemented: BOOLEAN;
  25.         gTheEvent: EventRecord;
  26.         gCurRow, gMaxRow: INTEGER;
  27.         gSizeRect: Rect;
  28.  
  29.  
  30. {-------------------------------->    CenterPict    <---}
  31.  
  32.     procedure CenterPict (thePicture: PicHandle; var myRect: Rect);
  33.         var
  34.             windRect, pictureRect: Rect;
  35.  
  36.     begin
  37.         windRect := myRect;
  38.         pictureRect := thePicture^^.picFrame;
  39.         myRect.top := (windRect.bottom - windRect.top - (pictureRect.bottom - pictureRect.top)) div 2 + windRect.top;
  40.         myRect.bottom := myRect.top + (pictureRect.bottom - pictureRect.top);
  41.         myRect.left := (windRect.right - windRect.left - (pictureRect.right - pictureRect.left)) div 2 + windRect.left;
  42.         myRect.right := myRect.left + (pictureRect.right - pictureRect.left);
  43.     end;
  44.  
  45.  
  46. {-------------------------------->    DrawMyPicture    <---}
  47.  
  48.     procedure DrawMyPicture (drawingWindow: WindowPtr);
  49.         var
  50.             drawingClipRect, myRect: Rect;
  51.             oldPort: GrafPtr;
  52.             tempRgn: RgnHandle;
  53.             thePicture: PicHandle;
  54.     begin
  55.         GetPort(oldPort);
  56.         SetPort(drawingWindow);
  57.         tempRgn := NewRgn;
  58.         GetClip(tempRgn);
  59.         EraseRect(drawingWindow^.portRect);
  60.         DrawGrowIcon(drawingWindow);
  61.  
  62.         drawingClipRect := drawingWindow^.portRect;
  63.         drawingClipRect.right := drawingClipRect.right - SCROLL_BAR_PIXELS;
  64.         drawingClipRect.bottom := drawingClipRect.bottom - SCROLL_BAR_PIXELS;
  65.         myRect := drawingWindow^.portRect;
  66.  
  67.         thePicture := GetPicture(BASE_RES_ID);
  68.         CenterPict(thePicture, myRect);
  69.         ClipRect(drawingClipRect);
  70.         DrawPicture(thePicture, myRect);
  71.  
  72.         SetClip(tempRgn);
  73.         DisposeRgn(tempRgn);
  74.         SetPort(oldPort);
  75.     end;
  76.  
  77.  
  78. {-------------------------------->    HandleMouseDown    <---}
  79.  
  80.     procedure HandleMouseDown;
  81.         var
  82.             whichWindow: WindowPtr;
  83.             thePart: INTEGER;
  84.             windSize: LONGINT;
  85.             oldPort: GrafPtr;
  86.     begin
  87.         thePart := FindWindow(gTheEvent.where, whichWindow);
  88.         case thePart of
  89.             inSysWindow: 
  90.                 SystemClick(gTheEvent, whichWindow);
  91.             inDrag: 
  92.                 DragWindow(whichWindow, gTheEvent.where, screenBits.bounds);
  93.             inContent: 
  94.                 if whichWindow <> FrontWindow then
  95.                     SelectWindow(whichWindow);
  96.             inGrow: 
  97.                 begin
  98.                     windSize := GrowWindow(whichWindow, gTheEvent.where, gSizeRect);
  99.                     if (windSize <> 0) then
  100.                         begin
  101.                             GetPort(oldPort);
  102.                             SetPort(whichWindow);
  103.                             EraseRect(whichWindow^.portRect);
  104.                             SizeWindow(whichWindow, LoWord(windSize), HiWord(windSize), NORMAL_UPDATES);
  105.                             InvalRect(whichWindow^.portRect);
  106.                             SetPort(oldPort);
  107.                         end;
  108.                 end;
  109.             inGoAway: 
  110.                 gDone := TRUE;
  111.             inZoomIn, inZoomOut: 
  112.                 if TrackBox(whichWindow, gTheEvent.where, thePart) then
  113.                     begin
  114.                         GetPort(oldPort);
  115.                         SetPort(whichWindow);
  116.                         EraseRect(whichWindow^.portRect);
  117.                         ZoomWindow(whichWindow, thePart, LEAVE_WHERE_IT_IS);
  118.                         InvalRect(whichWindow^.portRect);
  119.                         SetPort(oldPort);
  120.                     end;
  121.         end;
  122.     end;
  123.  
  124.  
  125. {-------------------------------->    ScrollWindow    <---}
  126.  
  127.     procedure ScrollWindow;
  128.         var
  129.             tempRgn: RgnHandle;
  130.     begin
  131.         tempRgn := NewRgn;
  132.         ScrollRect(gEventWindow^.portRect, HORIZONTAL_OFFSET, -ROWHEIGHT, tempRgn);
  133.         DisposeRgn(tempRgn);
  134.     end;
  135.  
  136.  
  137. {-------------------------------->    DrawEventString    <---}
  138.  
  139.     procedure DrawEventString (s: Str255);
  140.     begin
  141.         if (gCurRow > gMaxRow) then
  142.             ScrollWindow
  143.         else
  144.             gCurRow := gCurRow + ROWHEIGHT;
  145.  
  146.         MoveTo(LEFTMARGIN, gCurRow);
  147.         DrawString(s);
  148.     end;
  149.  
  150.  
  151. {-------------------------------->    HandleEvent    <---}
  152.  
  153.     procedure HandleEvent;
  154.         var
  155.             gotOne: BOOLEAN;
  156.     begin
  157.         if gWNEImplemented then
  158.             gotOne := WaitNextEvent(everyEvent, gTheEvent, SLEEP, nil)
  159.         else
  160.             begin
  161.                 SystemTask;
  162.                 gotOne := GetNextEvent(everyEvent, gTheEvent);
  163.             end;
  164.  
  165.         if gotOne then
  166.             case gTheEvent.what of
  167.                 nullEvent: 
  168.                     begin
  169.             {    DrawEventString('nullEvent');                            }
  170.             {    Uncomment the previous line for a burst of flavor!     }
  171.                     end;
  172.                 mouseDown: 
  173.                     begin
  174.                         DrawEventString('mouseDown');
  175.                         HandleMouseDown;
  176.                     end;
  177.                 mouseUp: 
  178.                     DrawEventString('mouseUp');
  179.                 keyDown: 
  180.                     DrawEventString('keyDown');
  181.                 keyUp: 
  182.                     DrawEventString('keyUp');
  183.                 autoKey: 
  184.                     DrawEventString('autoKey');
  185.                 updateEvt: 
  186.                     if (WindowPtr(gTheEvent.message) = gPictWindow) then
  187.                         begin
  188.                             DrawEventString('updateEvt: gPictWindow');
  189.                             BeginUpdate(WindowPtr(gTheEvent.message));
  190.                             DrawMyPicture(WindowPtr(gTheEvent.message));
  191.                             EndUpdate(WindowPtr(gTheEvent.message));
  192.                         end
  193.                     else
  194.                         begin
  195.                             DrawEventString('updateEvt: gEventWindow');
  196.                             BeginUpdate(WindowPtr(gTheEvent.message));
  197.                 {    We won't handle updates to gEventWindow,        }
  198.                 {    but we still need to empty the gEventWindow        }
  199.                 {    Update Region so the Window Manager will stop    }
  200.                 {    queing UpdateEvts.  We do this with calls to        }
  201.                 {    BeginUpdate and EndUpdate.                            }
  202.                             EndUpdate(WindowPtr(gTheEvent.message));
  203.                         end;
  204.                 diskEvt: 
  205.                     DrawEventString('diskEvt');
  206.                 activateEvt: 
  207.                     if (WindowPtr(gTheEvent.message) = gPictWindow) then
  208.                         begin
  209.                             DrawGrowIcon(WindowPtr(gTheEvent.message));
  210.                             if (BitAnd(gTheEvent.modifiers, activeFlag) = ACTIVATING) then
  211.                                 DrawEventString('activateEvt: activating gPictWindow')
  212.                             else
  213.                                 DrawEventString('activateEvt: deactivating gPictWindow');
  214.                         end
  215.                     else
  216.                         begin
  217.                             if (BitAnd(gTheEvent.modifiers, activeFlag) = ACTIVATING) then
  218.                                 DrawEventString('activateEvt: activating gEventWindow')
  219.                             else
  220.                                 DrawEventString('activateEvt: deactivating gEventWindow');
  221.                         end;
  222.                 networkEvt: 
  223.                     DrawEventString('networkEvt');
  224.                 driverEvt: 
  225.                     DrawEventString('driverEvt');
  226.                 app1Evt: 
  227.                     DrawEventString('app1Evt');
  228.                 app2Evt: 
  229.                     DrawEventString('app2Evt');
  230.                 app3Evt: 
  231.                     DrawEventString('app3Evt');
  232.                 app4Evt: 
  233.                     if (BitAnd(gTheEvent.message, SUSPEND_RESUME_BIT) = RESUMING) then
  234.                         DrawEventString('Resume event')
  235.                     else
  236.                         DrawEventString('Suspend event');
  237.             end;
  238.     end;
  239.  
  240.  
  241. {-------------------------------->    MainLoop    <---}
  242.  
  243.     procedure MainLoop;
  244.     begin
  245.         gDone := FALSE;
  246.         gWNEImplemented := (NGetTrapAddress(WNE_TRAP_NUM, ToolTrap) <> NGetTrapAddress(UNIMPL_TRAP_NUM, ToolTrap));
  247.  
  248.         while gDone = FALSE do
  249.             HandleEvent;
  250.     end;
  251.  
  252.  
  253. {-------------------------------->    SetUpSizeRect    <---}
  254.  
  255.     procedure SetUpSizeRect;
  256.     begin
  257.         gSizeRect.top := MIN_WINDOW_HEIGHT;
  258.         gSizeRect.left := MIN_WINDOW_WIDTH;
  259.  
  260.         gSizeRect.bottom := 32767;
  261.         gSizeRect.right := 32767;
  262.     end;
  263.  
  264.  
  265. {-------------------------------->    SetupEventWindow    <---}
  266.  
  267.     procedure SetupEventWindow;
  268.         var
  269.             eventRect: Rect;
  270.             fontNum: INTEGER;
  271.     begin
  272.         eventRect := gEventWindow^.portRect;
  273.         gMaxRow := eventRect.bottom - eventRect.top - ROWHEIGHT;
  274.         gCurRow := STARTROW;
  275.  
  276.         SetPort(gEventWindow);
  277.         GetFNum('monaco', fontNum);
  278.         TextFont(fontNum);
  279.         TextSize(TEXT_FONT_SIZE);
  280.     end;
  281.  
  282.  
  283. {-------------------------------->    WindowInit    <---}
  284.  
  285.     procedure WindowInit;
  286.     begin
  287.         gPictWindow := GetNewWindow(BASE_RES_ID, nil, WindowPtr(-1));
  288.         gEventWindow := GetNewWindow(BASE_RES_ID + 1, nil, WindowPtr(-1));
  289.  
  290.         SetupEventWindow;
  291.  
  292.         ShowWindow(gEventWindow);
  293.         ShowWindow(gPictWindow);
  294.     end;
  295.  
  296.  
  297. {-------------------------------->    EventTutor    <---}
  298.  
  299. begin
  300.     WindowInit;
  301.     SetUpSizeRect;
  302.  
  303.     MainLoop;
  304. end.